﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using GDFlacTool.Common;
using GDFlacTools.Controls.CoolPrintPreview;

namespace GDFlacTool.Forms
{
    public partial class FormPrintJewelCase : Form
    {
        private readonly IList<Track> _tracks;

        public FormPrintJewelCase(IList<Track> tracks)
        {
            InitializeComponent();

            _tracks = tracks;
        }

        private void printDocument_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
        {
            Graphics g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias;

            g.PageUnit = GraphicsUnit.Millimeter;

            DrawStandardBackJewelCase(g);
        }

        private void DrawStandardBackJewelCase(Graphics g)
        {
            Pen blackPen = new Pen(Color.Black, 0.3f);
            g.DrawRectangle(blackPen, 20, 20, 151f, 118f);
            
            blackPen.DashStyle = DashStyle.Dash;
            var p1 = new PointF(26.5f, 20f);
            var p2 = new PointF(26.5f, 138f);
            g.DrawLine(blackPen, p1, p2);

            p1 = new PointF(164.5f, 20f);
            p2 = new PointF(164.5f, 138f);
            g.DrawLine(blackPen, p1, p2);

            var drawFont = new Font("Arial", 9, FontStyle.Bold);
            var drawString = CreateBackCoverSideText();
            var drawBrush = new SolidBrush(Color.Black);
            var drawFormat = new StringFormat();

            drawFormat.Alignment = StringAlignment.Center;
            
            g.TranslateTransform(21.5f, 79f);
            g.RotateTransform(270);
            g.DrawString(drawString, drawFont, drawBrush, 0, 0, drawFormat);
            g.ResetTransform();

            g.TranslateTransform(169.5f, 79f);
            g.RotateTransform(90);
            g.DrawString(drawString, drawFont, drawBrush, 0, 0, drawFormat);
            g.ResetTransform();

            drawFont = new Font("Arial", 12, FontStyle.Bold);
            drawString = _tracks[0].Album;

            g.TranslateTransform(67f,-5f);
            g.RotateTransform(0);
            g.DrawString(drawString, drawFont, drawBrush, 30f, 30f, drawFormat);
            g.ResetTransform();
        }

        private int GetCurrentDisc(Track track)
        {
            return 1;
        }

        private int GetCurrentTrack(Track track)
        {
            return 1;
        }

        private int CheckNumberOfDiscs()
        {
            string disc;
            var chars = new []{'/'};
            int result;
            
            if (string.IsNullOrEmpty(_tracks[0].DiscNumber))
            {
                return 0;
            }

            string[] split = _tracks[0].DiscNumber.Split(chars, StringSplitOptions.RemoveEmptyEntries);

            if (split.Length == 1)
            {
                disc = split[0];
            }
            else if (split.Length == 2)
            {
                disc = split[1];
            }
            else
            {
                return 1;
            }

            if (int.TryParse(disc, out result))
            {
                return result;
            }

            return 1;
        }

        private string CreateBackCoverSideText()
        {
            string empty = "There are some missing tags! Please check.";
            string artist = _tracks[0].Artist;
            string album = _tracks[0].Album;
            string year = _tracks[0].Year;

            if (string.IsNullOrEmpty(artist) || string.IsNullOrEmpty(album) || string.IsNullOrEmpty(year))
            {
                return empty;
            }

            return string.Format("{0} - {1}  {2}", artist, album, year);
        }

        private void btnPreview_Click(object sender, EventArgs e)
        {
            using (var dlg = new CoolPrintPreviewDialog())
            {
                dlg.Width = 850;
                dlg.Height = 700;
                dlg.Text = @"Print Standard Jewel Case - Back";
                dlg.Document = printDocument;
                dlg.ShowDialog(this);
            }
        }
    }
}
